package com.scrcu.cloud.wechat.home.endpoint.staff.service.Iml;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scrcu.cloud.wechat.home.endpoint.common.model.RestResponse;
import com.scrcu.cloud.wechat.home.endpoint.customer.entity.Institution;
import com.scrcu.cloud.wechat.home.endpoint.customer.mapper.InstMapper;
import com.scrcu.cloud.wechat.home.endpoint.staff.entity.StaffInfo;
import com.scrcu.cloud.wechat.home.endpoint.staff.mapper.StaffInfoMapper;
import com.scrcu.cloud.wechat.home.endpoint.staff.service.IStaffOaService;
import com.scrcu.cloud.wechat.home.endpoint.staff.vo.StaffInfoPlus;
import lombok.extern.slf4j.Slf4j;
import com.scrcu.cloud.wechat.home.endpoint.staff.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
@Slf4j
public class StaffOaServiceIml extends ServiceImpl<StaffInfoMapper, StaffInfo> implements IStaffOaService {

    @Autowired
    private InstMapper instMapper;

    @Override
    public RestResponse getPersonInfo(String openid) {

        StaffInfo staffInfo = getBaseMapper().selectById(openid);
        String mapid = staffInfo.getMapId();
        Institution institution = instMapper.selectById(mapid);

        StaffInfoPlus staffInfoPlus = new StaffInfoPlus();
        staffInfoPlus.setInst("");
        if (institution!=null)
            staffInfoPlus.setInst(institution.getName());
        staffInfoPlus.setStaffId(staffInfo.getStaffId());
        staffInfoPlus.setCity(staffInfo.getCity());
        staffInfoPlus.setName(staffInfo.getName());
        staffInfoPlus.setOanum(staffInfo.getOanum());
        staffInfoPlus.setOpenid(staffInfo.getOpenid());
        staffInfoPlus.setCreateTime(staffInfo.getCreateTime());
        staffInfoPlus.setIdcard(staffInfo.getIdcard());
        staffInfoPlus.setMapId(staffInfo.getMapId());
        staffInfoPlus.setPriority(staffInfo.getPriority());
        staffInfoPlus.setTelnum(staffInfo.getTelnum());
        staffInfoPlus.setOrcode(staffInfo.getOrcode());
        staffInfoPlus.setUserid(staffInfo.getUserid());
        staffInfoPlus.setPost(staffInfo.getPost());
        staffInfoPlus.setDepartment(staffInfo.getDepartment());
        staffInfoPlus.setArea(staffInfo.getArea());

        return RestResponse.success(staffInfoPlus);
    }

    @Override
    public RestResponse changePersonInfo(String openid, String mopenid,Integer todoType, String value) {

        HashMap<String,Object> res = new HashMap<>();
        StaffInfo staffInfo = getBaseMapper().selectById(openid);
        if (staffInfo==null)
            return RestResponse.failed("没找到员工信息，请核对");

        StaffInfo mstaffInfo = getBaseMapper().selectById(mopenid);
        if(mstaffInfo==null){
            return RestResponse.failed("没找到将要修改的员工数据，请核对");
        }

        switch (todoType){
            case 0: //修改姓名
                if (value!=null&&value.length()>1){
                    mstaffInfo.setName(value);
                }else{
                    return RestResponse.failed("姓名输入错误");
                }
                break;
            case 1: //修改手机号
                if (value!=null&& Util.isNumeric(value)){
                    mstaffInfo.setTelnum(value);
                }else{
                    return RestResponse.failed("格式错误");
                }
                break;
            case 2: //修改工号
                if (value!=null&& Util.isNumeric(value)){
                    mstaffInfo.setOanum(value);
                }else{
                    return RestResponse.failed("格式错误");
                }
                break;
            case 3: //修改身份证
                if (value!=null&& Util.isNumeric(value)){
                    mstaffInfo.setIdcard(value);
                }else{
                    return RestResponse.failed("格式错误");
                }
                break;
            case 4: //城市和地区信息修改
                if (value != null && value.contains("-")){
                    String[] list = value.split("-");
                    if (list.length==2){
                        mstaffInfo.setCity(list[0]);
                        mstaffInfo.setArea(list[1]);
                    }
                }
                break;
            case 5: //机构信息修改
                if (value!=null&&value.length()>0){
                    if (instMapper.selectById(value)!=null){
                        mstaffInfo.setMapId(value);
                    }
                }
                break;
            case 6: //部门信息修改
                if (value!=null&& value.length()>0){
                    mstaffInfo.setDepartment(value);
                }else{
                    return RestResponse.failed("格式错误");
                }
                break;
            case 7: //岗位信息修改
                if (value!=null&& value.length()>0){
                    mstaffInfo.setPost(value);
                }else{
                    return RestResponse.failed("格式错误");
                }
                break;
            default:
                break;
        }

        int Flag = getBaseMapper().updateById(mstaffInfo);
        if (Flag>=1){
            StaffInfoPlus staffInfoPlus = new StaffInfoPlus();
            Institution institution = instMapper.selectById(mstaffInfo.getMapId());
            staffInfoPlus.setInst("");
            if (institution!=null)
                staffInfoPlus.setInst(institution.getName());
            staffInfoPlus.setStaffId(mstaffInfo.getStaffId());
            staffInfoPlus.setCity(mstaffInfo.getCity());
            staffInfoPlus.setName(mstaffInfo.getName());
            staffInfoPlus.setOanum(mstaffInfo.getOanum());
            staffInfoPlus.setOpenid(mstaffInfo.getOpenid());
            staffInfoPlus.setCreateTime(mstaffInfo.getCreateTime());
            staffInfoPlus.setIdcard(mstaffInfo.getIdcard());
            staffInfoPlus.setMapId(mstaffInfo.getMapId());
            staffInfoPlus.setPriority(mstaffInfo.getPriority());
            staffInfoPlus.setTelnum(mstaffInfo.getTelnum());
            staffInfoPlus.setOrcode(mstaffInfo.getOrcode());
            staffInfoPlus.setUserid(mstaffInfo.getUserid());
            staffInfoPlus.setPost(mstaffInfo.getPost());
            staffInfoPlus.setDepartment(mstaffInfo.getDepartment());
            staffInfoPlus.setArea(mstaffInfo.getArea());
            return RestResponse.success(staffInfoPlus);
        }
        return RestResponse.failed();
    }
}
